package com.zx.cloud.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zx.cloud.ApiVersion;
import com.zx.cloud.common.FreeMarkerFactory;
import com.zx.cloud.common.auth0.jwt.Token;
import com.zx.cloud.common.auth0.jwt.TokenGenerator;
import com.zx.cloud.common.auth0.jwt.exceptions.JWTCreationException;
import com.zx.cloud.common.filter.CurrentUserId;
import com.zx.cloud.common.filter.IgnoreAuth;
import com.zx.cloud.common.filter.MagicConstants;
import com.zx.cloud.config.ConfigUtil;
import com.zx.cloud.model.request.ManagerLoginRequest;
import com.zx.cloud.model.response.ObjectDataResponse;
import com.zx.cloud.util.CookieManager;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping(value = ApiVersion.PROVIDER_V1_PREFIX+"/demo")
public class DemoController{

	@Autowired
	private FreeMarkerFactory freeMarkerFactory;
	@Autowired
	private ConfigUtil configUtil;
	
	private static final Logger logger=Logger.getLogger(DemoController.class);
	
	@ApiOperation(value = "测试接口", notes = "测试接口")
	@RequestMapping(value = "/test", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<ManagerLoginRequest> test(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody ManagerLoginRequest loginRequest)  {
		System.out.println("--------------------test------------------------userId-["+userId+"]");
		loginRequest.setUsername(loginRequest.getUsername()+"----login");
		return new ObjectDataResponse<ManagerLoginRequest>(loginRequest);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "测试接口", notes = "测试接口")
	@RequestMapping(value = "/test5", method = RequestMethod.GET)
	@ResponseBody
	public ObjectDataResponse<ManagerLoginRequest> test5(@RequestParam String  username)  {
		return new ObjectDataResponse<ManagerLoginRequest>(null);
	}

	@IgnoreAuth
	@ApiOperation(value = "测试接口", notes = "测试接口")
	@RequestMapping(value = "/freemarker", method = RequestMethod.GET)
	@ResponseBody
	public ObjectDataResponse<String> freemarker() throws IOException  {
		String filename="test.html";
		Map<String, Object> data=new HashMap<String, Object>();
		data.put("title", "hahahaha");
		data.put("content", "this is news");
		System.out.println(" "+ResourceUtils.getFile("classpath:static/news.ftl").getAbsolutePath());
		freeMarkerFactory.createHTML(ResourceUtils.getFile("classpath:static/news.ftl").getAbsolutePath(), filename, data);
		String httpUrl=configUtil.getUploadAbsoluteFilePath()+"/"+filename;
		return new ObjectDataResponse<String>(httpUrl);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "测试接口", notes = "测试接口")
	@RequestMapping(value = "/test3", method = RequestMethod.GET)
	@ResponseBody
	public ObjectDataResponse<ManagerLoginRequest> test4()  {
		return new ObjectDataResponse<ManagerLoginRequest>(null);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "测试接口", notes = "测试接口")
	@RequestMapping(value = "/test3", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<ManagerLoginRequest> test3()  {
		return new ObjectDataResponse<ManagerLoginRequest>(null);
	}
	
	@ApiOperation(value = "测试接口", notes = "测试接口")
	@RequestMapping(value = "/test2", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<ManagerLoginRequest> test2(@ApiParam(hidden=true,name="登录用户ID")@CurrentUserId Long userId,@RequestBody ManagerLoginRequest loginRequest)  {
		System.out.println("--------------------test2------------------------userId-["+userId+"]");
		loginRequest.setUsername(loginRequest.getUsername()+"----login----["+userId);
		return new ObjectDataResponse<ManagerLoginRequest>(loginRequest);
	}
	
	@IgnoreAuth
	@ApiOperation(value = "测试管理系统用户登录", notes = "测试管理系统用户登录")
	@RequestMapping(value = "/loginTest", method = RequestMethod.POST)
	@ResponseBody
	public ObjectDataResponse<Token> loginTest(HttpServletRequest request,HttpServletResponse response,@RequestBody ManagerLoginRequest loginRequest) throws NoSuchAlgorithmException, InvalidKeySpecException, IllegalArgumentException, JWTCreationException, UnsupportedEncodingException{
		Map<String, Object> claims=new HashMap<String, Object>();
    	claims.put(MagicConstants.SESSION_USER_ID,1);
    	claims.put(MagicConstants.SESSION_USER_PERMISSIONS,null);
    	Token token=TokenGenerator.create(claims);
    	new CookieManager(request, response).setCookie(MagicConstants.AUTH_TOKEN, token.getToken(), 7*24*60*60);
		return new ObjectDataResponse<Token>(token);
	}
	
}
